
/**
 * name:        个人中心--首页
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   bgemall.com
 */
// 会员个人中心首页
$(function(){
    // 个人中心首页

    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token=''     
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            getMemberDetail(token,URL,name); 
            favoritesDetail(token,URL,name);
            orderStatus(token,URL);
            Logistics(token,URL);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){
                    tc(data.message)
                }  
            })
            setTimeout(function(){
                window.location.href='/index.html'
            },1100);
    });
});

// 首页基本资料
function getMemberDetail(token,URL,name){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_index/getMemberDetail',
        dataType:'json',
        data:{token:token},  
        success:function(e){ 
            if(e.code=='0'){
                memberDetailTop(e,name)
                var memberInfoTpl = $("#memberInfoTpl").html();
                var memberInfoCont=template(
                    memberInfoTpl, {
                        memberInfoList: [{
                            availablePredeposit:e.data.availablePredeposit,     // 账户余额
                            avatar:e.data.avatar,        // 会员头像
                            memberId:e.data.memberId,       // 会员Id
                            name:e.data.name,       // 会员名称
                            nickName:e.data.nickName,       // 会员昵称
                            type:e.data.type,   // 用户类型
                            points:e.data.points,       // 账户积分
                        }]
                    }
                );
                $('.mod_home_top').append(memberInfoCont);
            }else{
                tc(e.message);
                setTimeout(function(){
                        $.cookie('user','',{path:'/'})
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            } 
        }
    });
}

function memberDetailTop(e,name){
    if(e.data.name=='' || e.data.name ==null || e.data.nickName=='' || e.data.nickName ==null){
        e.data.name=name;
        e.data.nickName=name;
    };
    if(e.data.type==2){
        e.data.type='企业用户';
    }else{
        e.data.type='个人用户';
    }
    if(e.data.availablePredeposit=='' || e.data.availablePredeposit==null || e.data.points=='' || e.data.points==null){
        e.data.availablePredeposit='0.00';
        e.data.points='0';
    }
 }
// 我的收藏
function favoritesDetail(token,URL,name){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_index/getMemberDetail',
        dataType:'json',
        data:{token:token},  
        success:function(e){ 
            if(e.code=='0'){
                var favoritesListTpl = $("#favoritesListTpl").html();
                if( e.data.favoritesList == '' || e.data.favoritesList==null){
                    $('.home_like_wrap ul').append('<div style="width:100%;height:200px;line-height:200px;text-align:center;color:#999;">您暂时还没有收藏商品</div>');
                }else{
                    for(var i=0;i<e.data.favoritesList.length;i++){
                        var favoritesListCont=template(
                            favoritesListTpl, {
                                favoritesList: [{
                                    goodsId:e.data.favoritesList[i].goodsId,
                                    image:e.data.favoritesList[i].image,
                                    marketPrice:e.data.favoritesList[i].marketPrice,
                                    name:e.data.favoritesList[i].name,
                                    unitPrice:e.data.favoritesList[i].unitPrice,
                                    isDelete:e.data.favoritesList[i].isDelete,
                                    state:e.data.favoritesList[i].state
                                }]
                            }
                        );
                        $('.home_like_wrap .home_like_list ul').append(favoritesListCont);
                        var favorites_status=document.getElementsByClassName('favorites_status')
                        var home_like_pic=document.getElementsByClassName('home_like_pic')
                        // 是否商家商家删除or下架
                        $('.home_like_list ul li').each(function(){
                            if($(this).attr('data-state')=='0'){
                                $(this).find('.favorites_status').show().text('已下架').css({
                                    'text-align': 'center',
                                    'line-height': '100px',
                                    'font-size':'18px',
                                    'color':'#fff',
                                    'font-weight':'600',
                                    'top':'34px',
                                    'left':'34px'
                                })
                            }
                            if($(this).attr('data-isDelete')=='1'){
                                $(this).find('.favorites_status').show().text('已删除').css({
                                    'text-align': 'center',
                                    'line-height': '100px',
                                    'font-size':'18px',
                                    'color':'#fff',
                                    'font-weight':'600',
                                    'top':'34px',
                                    'left':'34px'
                                })
                            }
                        })
                    }
                }
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            }
        }
    });
 }
// 我的订单状态
function orderStatus(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_index/getMemberDetail',
        dataType:'json',
        data:{token:token},  
        success:function(e){
            if(e.code=='0'){
                var orderStatusTpl = $("#orderStatusTpl").html();
                var orderStatusCont=template(
                    orderStatusTpl, {
                        orderStatusList: [{
                            unpaid:e.data.statisticsData.unpaid,                    // 待付款
                            notReceiving:e.data.statisticsData.notReceiving,        // 待收货
                            unsent:e.data.statisticsData.unsent,                    // 待发货
                            refund:e.data.statisticsData.refund,                    // 售后/维权
                        }]
                    }
                );
                $('.home_order_status').append(orderStatusCont);
                if(e.data.orderInfo.length == '0'){
                    $('.home_order_wrap .home_order_list').append('<div style="width:100%;height:100px;line-height:100px;text-align:center;color:#999;">近期无订单</div>');
                }else{
                    var orderInfoTpl = $("#orderInfoTpl").html();
                    for(var i=0;i < 1; i++){
                        if(e.data.orderInfo.ordergoodsInfo[i].spec=='' || e.data.orderInfo.ordergoodsInfo[i].spec==null){
                            e.data.orderInfo.ordergoodsInfo[i].spec = '默认';
                        }
                        var orderInfoCont=template(
                            orderInfoTpl, {
                                orderInfoList: [{
                                    orderId:e.data.orderInfo.orderId,
                                    goodsId:e.data.orderInfo.ordergoodsInfo[i].goodsId,
                                    image:e.data.orderInfo.ordergoodsInfo[i].image,
                                    name:e.data.orderInfo.ordergoodsInfo[i].name,
                                    num:e.data.orderInfo.ordergoodsInfo[i].num,
                                    spec:e.data.orderInfo.ordergoodsInfo[i].spec,
                                    unitPrice:e.data.orderInfo.ordergoodsInfo[i].unitPrice,
                                }]
                            }
                        );
                        $('.home_order_wrap .home_order_list').append(orderInfoCont);
                    }
                    var time = e.data.orderInfo.createTime;
                    $(".home_order_num").text('订单号：'+e.data.orderInfo.orderNo);
                    $(".home_order_time").text(getLocalTime(time));
                    $(".home_order_price").text(e.data.orderInfo.totalPrice);
                }
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            } 
        }
    });
}
// 我的物流
function Logistics(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_index/getMemberDetail',
        dataType:'json',
        data:{token:token},  
        success:function(e){
            if(e.code=='0'){
                if( e.data.logistics == ""){  
                    $('.home_wuliu_wrap .mod_home_wuliu').append('<div style="width:100%;height:100px;line-height:100px;text-align:center;color:#999;">暂无物流信息</div>');
                }else{
                    var time = e.data.logistics.time;
                    var wuliuTpl = $("#wuliuTpl").html();
                    var wuliuCont=template(
                        wuliuTpl, {
                            wuliuList: [{
                                time:time,
                                information:e.data.logistics.information,
                            }]
                        }
                    );
                    $('.home_wuliu_wrap .mod_home_wuliu').append(wuliuCont);
                    $('.home_wuliu_pic img').attr('src',e.data.orderInfo.ordergoodsInfo[0].image);
                    $(".home_order_txt").text(e.data.orderInfo.ordergoodsInfo[0].name);
                    $(".home_wuliu_view, .home_view_more").click(function(){
                        view();
                        var orderId = $(".home_order_list .five").attr("data-order-id");
                        $.ajax({
                            type:'post',
                            url:new_url+'shop/member_order/getLogisticsInfo',
                            dataType:'json',
                            data:{token:token,orderId:orderId},   
                            success:function(response){ 
                                if(response.code=='0'){
                                    for(var t=0; t<response.data.logList.length; t++){  
                                        $('.logistics_list ul').append('<li class="text"></li>')
                                        var text=document.getElementsByClassName('text')
                                        text[t].innerHTML='<span>'+response.data.logList[t].time+'</span>'+response.data.logList[t].context
                                    }
                                    $('.logistics_origin').append('信息来源：<label>'+response.data.name+'</label>');
                                    $('.logistics_num').text('运单号：'+response.data.logisticsNo);
                                }else{
                                    $('.logistics_list ul').append('<li class="text">获取物流信息失败</li>');
                                }
                            }
                        })
                    })
                }
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            }
        }
    });
}



function getLocalTime(nS) {     
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
}
// 关闭弹窗
function close_popup(){
    $(".popup_bg, .close, .cancel").click(function(){
        $(".popup_bg").css({"display":"none"});
        $(".popup_wrap").remove();
    });
}

// 查看物流
function view(){
    $(".popup_bg").css({"display":"block","height":"100%"});
    var t='';
    t +='<div class="popup_wrap popup_one">';
    t +='<div class="popup">';
    t +='<div class="popup_title">';
    t +='<span class="title_txt">物流信息</span>';
    t +='<span class="close">X</span>';
    t +='</div>';
    t +='<div class="popup_content">';
    t +='<p class="logistics_title">';
    t +='<span class="logistics_origin"></span>';
    t +='<span class="logistics_num"></span>';
    t +='</p>';
    t +='<div class="logistics_list">';
    t +='<ul>';
    t +='</ul>';
    t +='</div>';
    t +='</div>';
    t +='</div>';
    t +='</div>';
    $('body').append(t);
    close_popup();
}
